package utilities;

import java.math.*;

public class Orezanie {

	int i, j, p, polohaBodky, cislica, bodka, d;
	String s = "", s1;
	String poslednaCislica;
	char[] pole;
	BigDecimal bd;
	
	public String orez(String ret, String retZTF){

		pole = new char[ret.length()];
		p = zistiPresnost(retZTF);
		
		for(i = 0; i < ret.length(); i++){
			
			pole[i] = ret.charAt(i);
			if(ret.charAt(i) == '.'){
				
				bodka = i;
				
			}
		}
		if(((bodka + p + 1) < pole.length) && (pole[bodka + p + 1] == '0')){
			
			s = "";
			for(i = 0; i < (bodka + p + 1); i++){
				
				s += String.valueOf(pole[i]); 
				
			}
		}
		if(((bodka + p + 1) < pole.length) && (pole[bodka + p + 1] == '9')){
			
			s = "";
            bd = new BigDecimal(Double.valueOf(ret).doubleValue());
            bd = bd.setScale(p, BigDecimal.ROUND_HALF_UP);
            s = String.valueOf(bd.doubleValue());
			
		}

/*		
		if(((bodka + p + 1) < pole.length) && (pole[bodka + p + 1] != '0') && (pole[bodka + p + 1] != '9')){

			s = ret;
			
		}
*/		
		if((bodka + p + 1) > (pole.length - 1)){
			
			s = ret;
			
		}
		return s;
	}
	
	public int zistiPresnost(String retZTF){
	
		for(j = 0; j < retZTF.length(); j++){
			
			if(retZTF.charAt(j) == '.'){
				
				polohaBodky = j;
				
			}
		}
        return (retZTF.length() - polohaBodky - 1);
	}
	public String zistiIntAleboDouble(String r) {
		
		d = 0;
		for(j = 0; j < r.length(); j++){			
			if(r.charAt(j) == '.'){
				d++;
				s1 = String.valueOf((int) Double.valueOf(r).doubleValue());
				break;				
			}
		}
		if(d != 1){			
			s1 = r;			
		}
		return s1;
	}
}
